package com.bayair.doctor.biz.im_custom;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.tencent.imsdk.TIMConnListener;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMGroupEventListener;
import com.tencent.imsdk.TIMGroupTipsElem;
import com.tencent.imsdk.TIMLogLevel;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMMessageListener;
import com.tencent.imsdk.TIMRefreshListener;
import com.tencent.imsdk.TIMSdkConfig;
import com.tencent.imsdk.TIMUserConfig;
import com.tencent.imsdk.TIMUserStatusListener;
import com.tencent.imsdk.ext.message.TIMMessageLocator;
import com.tencent.imsdk.ext.message.TIMMessageRevokedListener;
import com.tencent.imsdk.session.SessionWrapper;

import java.util.List;

/**
 * 初始化
 * 包括imsdk等
 */
public class InitBusiness {

    private static final String TAG = InitBusiness.class.getSimpleName();

    private InitBusiness() {
    }

    public static void start(Context context) {
        initImsdk(context, TIMLogLevel.VERBOSE);
    }

    public static void start(Context context, int logLevel) {
        initImsdk(context, logLevel);
        initUserConfig();
    }

    /**
     * 初始化imsdk
     */
    private static void initImsdk(Context context, int logLevel) {
        //判断是否是在主线程
        if (SessionWrapper.isMainProcess(context)) {
            TIMSdkConfig config = new TIMSdkConfig(1400252027)
                    .enableLogPrint(true)
                    .setLogLevel(logLevel)
                    .setLogPath(Environment.getExternalStorageDirectory().getPath() + "/xiaoke/imlog/");

            //初始化 SDK
            boolean init = TIMManager.getInstance().init(context, config);
            //禁止服务器自动代替上报已读
            Log.d(TAG, "initIMsdk" + init);
        }
    }

    private static void initUserConfig() {
        //基本用户配置
        TIMUserConfig userConfig = new TIMUserConfig()
                .setUserStatusListener(new TIMUserStatusListener() {
                    @Override
                    public void onForceOffline() {
                        //被其他终端踢下线
                        Log.i(TAG, "onForceOffline");
                        // App.TOKEN = "";
                        // UserControl.getInstance().clear();
                        // DataCleanManager.clearAllCache(getContext());
                        // PageRouter.startLogin(getContext());
                        // finish();
                    }

                    @Override
                    public void onUserSigExpired() {
                        //用户签名过期了，需要刷新userSig重新登录SDK
                        Log.i(TAG, "onUserSigExpired");
                    }
                })
                //设置连接状态事件监听器
                .setConnectionListener(new TIMConnListener() {
                    @Override
                    public void onConnected() {
                        Log.i(TAG, "onConnected");
                    }

                    @Override
                    public void onDisconnected(int code, String desc) {
                        Log.i(TAG, "onDisconnected");
                    }

                    @Override
                    public void onWifiNeedAuth(String name) {
                        Log.i(TAG, "onWifiNeedAuth");
                    }
                })
                //设置群组事件监听器
                .setGroupEventListener(new TIMGroupEventListener() {
                    @Override
                    public void onGroupTipsEvent(TIMGroupTipsElem elem) {
                        Log.i(TAG, "onGroupTipsEvent, type: " + elem.getTipsType());
                    }
                })
                //设置会话刷新监听器
                .setRefreshListener(new TIMRefreshListener() {
                    @Override
                    public void onRefresh() {
                        Log.i(TAG, "onRefresh");
                    }

                    @Override
                    public void onRefreshConversation(List<TIMConversation> conversations) {
                        Log.i(TAG, "onRefreshConversation, conversation size: " + conversations.size());
                    }
                })
                //设置消息撤回通知监听器
                .setMessageRevokedListener(new TIMMessageRevokedListener() {
                    @Override
                    public void onMessageRevoked(TIMMessageLocator timMessageLocator) {

                    }
                });

        //RefreshEvent.getInstance().init(userConfig);
        //userConfig = FriendshipEvent.getInstance().init(userConfig);
        //userConfig = MessageEvent.getInstance().init(userConfig);
        //userConfig = GroupEvent.getInstance().init(userConfig);

        //新消息通知
        TIMManager.getInstance().addMessageListener(new TIMMessageListener() {
            /**
             * 收到新消息回调
             * @param msgs 收到的新消息
             * @return 正常情况下，如果注册了多个listener, IM SDK会顺序回调到所有的listener。当碰到listener的回调返回true的时候，将终止继续回调后续的listener。
             */
            @Override
            public boolean onNewMessages(List<TIMMessage> msgs) {
//                for (IMEventListener l : sIMEventListeners) {
//                    l.onNewMessages(msgs);
//                }
                return false;
            }
        });
        //userConfig.disableStorage(); //禁用本地所有存储
        userConfig.enableReadReceipt(true);//开启消息已读回执
        //将用户配置与通讯管理器进行绑定
        TIMManager.getInstance().setUserConfig(userConfig);

    }


    public interface OffLineNotify {
        void exit();
    }

}
